/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted




/// Generating total religion respondants by year

by year: egen Religionresp = count(religion) if religion~=4

/// Generating total number of religion voters by year


by year: egen religionvoted = count(voted) if religion==4 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen religionNvoted = count(voted) if voted==0 & religion==4 | voted==1 & religion==4 | voted==2 & religion==4

/// Generating total number of non-religion voters by year

by year: egen Nreligionvoted = count(voted) if religion~=4 & voted==3

/// Generating total number of non-religion non-voters by year

by year: egen NreligionNvoted = count(voted) if religion~=4 & voted==0 | religion~=4 & voted==1 | religion~=4 & voted==2

/// filling in the blanks

by year: egen Religionvoted = max(religionvoted)
by year: egen ReligionNvoted = max(religionNvoted)
by year: egen NReligionvoted = max(Nreligionvoted)
by year: egen NReligionNvoted = max(NreligionNvoted)




/// Generating % religion TO with Mosteller Correction

gen N = 1
gen a = [Religionvoted * NReligionNvoted] / [ReligionNvoted * NReligionvoted]


///Generating filler vars

gen aminone =(a-1)
gen AnonreligionplusTO = (Anonreligion+aTO)
gen areligionTO=(a*Anonreligion*aTO)

gen CreligionTO = 1/ (2*(aminone)) * (N+(aminone*AnonreligionplusTO) - sqrt((N+aminone*AnonreligionplusTO)*(N+aminone*AnonreligionplusTO) - (4*(aminone*areligionTO))))

///Adjusting TO for the formula

replace CreligionTO = CreligionTO/Anonreligion



/// Moving to Partisanship - Generating religion Democratic Vote total by year

by year: egen religiondem = count(religion) if presvote==1 & religion==4

/// Generating religion Non-Democratic Vote by Year

by year: egen religionNdem = count(religion) if presvote==2 & religion==4

// Generating non-religion Democratic Vote total by year

by year: egen Nreligiondem = count(religion) if presvote==1 & religion~=4

/// Generating non-religion non-Democratic vote total by year

by year: egen NreligionNdem = count(religion) if presvote==2 & religion~=4

/// filling in the blanks

by year: egen ReligionDem = max(religiondem)
by year: egen ReligionNDem = max(religionNdem)
by year: egen NReligionDem = max(Nreligiondem)
by year: egen NReligionNDem = max(NreligionNdem)

/// Generation % religion Democrat with Mosteller Correction

gen A = (ReligionDem*NReligionNDem)/(ReligionNDem*NReligionDem)
gen Aminone =(A-1)
gen AnonreligionplusDem = (Anonreligion+ADem)
gen AnonreligionDem=(A*Anonreligion*ADem)
gen CreligionDem = 1/(2*(Aminone)) * (N+(Aminone*AnonreligionplusDem) - sqrt((N+Aminone*AnonreligionplusDem)*(N+Aminone*AnonreligionplusDem) - (4*(Aminone*AnonreligionDem))))

replace CreligionDem = CreligionDem/Anonreligion


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen Religioncoalcontrib = (CreligionTO* CreligionDem *Anonreligion)/(aTO*ADem)


